--传送门

local PortalFactory = class("PortalFactory")

function PortalFactory:ctor(bound, node)
	self.bound = bound
	self.node = node

	math.random(os.time())

	self:Generate()
end

function PortalFactory:Generate( )
	if self.portal ~= nil then
		self.node:removeChild(self.portala.sp)
		self.node:removeChild(self.portalb.sp)
	end
	self.portala = self:createOnePortal()
	self.portalb = self:createOnePortal()

	SetCollide(self.portala.x, self.portala.y, { Name = "portala", Type = "portal", link = self.portalb })
	SetCollide(self.portalb.x, self.portalb.y, { Name = "portalb", Type = "portal", link = self.portala })
end

function PortalFactory:createOnePortal( )
	local sp = cc.Sprite:create("portal.png")	

	local x, y = RandomEmptyPos(self.bound)
	--逻辑坐标转成视图坐标
	local finalX, finalY = Grid2Pos(x, y)
	sp:setPosition(finalX, finalY)
	self.node:addChild(sp)

	return { ["x"] = x, ["y"] = x, ["sp"] = sp }
end

function PortalFactory:Reset( )
	self.node:removeChild(self.portala.sp)
	self.node:removeChild(self.portalb.sp)
end

return PortalFactory